import java.io.*;

public class Clauses 
{
	int i = 0;
	int j = 0;
	int k = 0;
	int m = 0;
	int n = 0;
	int counter = 0;
	
	FileWriter fstream;
	BufferedWriter out;
	
	String input;
	
	String[][][] squares = new String[9][9][9];
	
	String[] uniquenessClauses = new String [648];
	String[] rowClauses = new String [4536];
	String[] columnClauses = new String [4536];
	String[] boxClauses = new String [4536];
	
	public void convert()
	{		
		//Individual squares to 123 format
		for(i=0;i<9;i++)
		{
			for(j=0;j<9;j++)
			{
				for(k=0;k<9;k++)
				{
					squares[i][j][k] = Integer.toString(i+1)+Integer.toString(j+1)+Integer.toString(k+1);
				}
			}
		}
		
		counter = 0;
		for(i=0;i<9;i++)
		{
			for(j=0;j<9;j++)
			{
				input = squares[i][j][0];
				
				for(k=1;k<9;k++)
				{
					input = input + " " + squares[i][j][k];
				}
				
				input = input + " 0";
				
				uniquenessClauses[counter] = input;
				
				counter++;
			}
		}
		
		//Making sure no two squares in a row contain the same thing.
		counter = 0;
		for(i=0; i<9; i++)
		{
			for(j=0; j<9; j++)
			{
				for(k=0; k<8; k++)
				{
					for(m=i+1; m<9; m++)
					{
						input = "-" + squares[i][j][k] + " -" + squares[m][j][k] + " 0";
						
						rowClauses[counter] = input;
						counter++;
					}
				}
			}
		}
		
		
		//Making sure no two squares in a column contain the same number.
		counter = 0;
		for(i=0; i<9; i++)
		{
			for(j=0; j<9; j++)
			{
				for(k=0; k<8; k++)
				{
					for(m=i+1; m<9; m++)
					{
						input = "-" + squares[i][j][k] + " -" + squares[i][m][k] + " 0";
						
						columnClauses[counter] = input;
						counter++;
					}
				}
			}
		}
			
		
		//Making sure no two boxes in the same square of 9 have the same number.
		counter =0;
		for(i=0; i<9; i++)
		{
			for(j=0; j<9; j++)
			{
				for(k=0; k<9; k++)
				{
					if(i<3)
					{
						if(j<3)
						{
							for(m=0;m<3;m++)
							{
								for(n=0;n<3;n++)
								{
									if(m != i && n != j)
									{
										input = "-" + squares[i][j][k] + " -" + squares[m][n][k] + " 0";
										boxClauses[counter] = input;
										counter++;
									}
								}
							}
						}
						
						else if(j<6)
						{
							for(m=0;m<3;m++)
							{
								for(n=3;n<6;n++)
								{
									if(m != i && n != j)
									{
										input = "-" + squares[i][j][k] + " -" + squares[m][n][k] + " 0";
										boxClauses[counter] = input;
										counter++;
									}
								}
							}
						}
						
						else if(j<9)
						{	
							for(m=0;m<3;m++)
							{
								for(n=6;n<9;n++)
								{
									if(m != i && n != j)
									{
										input = "-" + squares[i][j][k] + " -" + squares[m][n][k] + " 0";
										boxClauses[counter] = input;
										counter++;
									}
								}
							}
						}
					}
					
					else if(i<6)
					{	
						if(j<3)
						{
							for(m=3;m<6;m++)
							{
								for(n=0;n<3;n++)
								{
									if(m != i && n != j)
									{
										input = "-" + squares[i][j][k] + " -" + squares[m][n][k] + " 0";
										boxClauses[counter] = input;
										counter++;
									}
								}
							}
						}
						
						else if(j<6)
						{
							for(m=3;m<6;m++)
							{
								for(n=3;n<6;n++)
								{
									if(m != i && n != j)
									{
										input = "-" + squares[i][j][k] + " -" + squares[m][n][k] + " 0";
										boxClauses[counter] = input;
										counter++;
									}
								}
							}
						}
						
						else if(j<9)
						{	
							for(m=3;m<6;m++)
							{
								for(n=6;n<9;n++)
								{
									if(m != i && n != j)
									{
										input = "-" + squares[i][j][k] + " -" + squares[m][n][k] + " 0";
										boxClauses[counter] = input;
										counter++;
									}
								}
							}
						}
					}
					else if(i<9)
					{	
						if(j<3)
						{
							for(m=6;m<9;m++)
							{
								for(n=0;n<3;n++)
								{
									if(m != i && n != j)
									{
										input = "-" + squares[i][j][k] + " -" + squares[m][n][k] + " 0";
										boxClauses[counter] = input;
										counter++;
									}
								}
							}
						}
					
						else if(j<6)
						{
							for(m=6;m<9;m++)
							{
								for(n=3;n<6;n++)
								{
									if(m != i && n != j)
									{
										input = "-" + squares[i][j][k] + " -" + squares[m][n][k] + " 0";
										boxClauses[counter] = input;
										counter++;
									}
								}
							}
						}
					
						else if(j<9)
						{	
							for(m=6;m<9;m++)
							{
								for(n=6;n<9;n++)
								{
									if(m != i && n != j)
									{
										input = "-" + squares[i][j][k] + " -" + squares[m][n][k] + " 0";
										boxClauses[counter] = input;
										counter++;
									}
								}
							}
						}	
					}	
				}
			}
		}
	}
	
	
	public void writeToFile()
	{
			System.out.println("Trying to make a file");
			try
			{
				System.out.println("in try");
				// Create file 
				fstream = new FileWriter("test.txt");
				out = new BufferedWriter(fstream);			        		       

				for(int i=0; i < uniquenessClauses.length; i++)
				{			        	
					out.write(i+ " ");
					out.write(uniquenessClauses[i]);
					out.newLine();
				}
				
				for(int i=0; i < rowClauses.length; i++)
				{			        	
					out.write(i+ " ");
					out.write(rowClauses[i]);
					out.newLine();
				}
				
				for(int i=0; i < columnClauses.length; i++)
				{			        	
					out.write(i+ " ");
					out.write(columnClauses[i]);
					out.newLine();
				}
				
				for(int i=0; i < boxClauses.length; i++)
				{			        	
					out.write(i+ " ");
					out.write(boxClauses[i]);
					out.newLine();
				}
				System.out.println("out of for");
				out.write("endoffile");

				//Close the output stream
				out.close();
			}
			catch (Exception e)
			{
				System.out.println("In catch");
				//Catch exception if any
				System.err.println("Encountered an Error : " + e.getMessage() + "\n");
			}
		
		 
		 finally
		 {
			    //Close the output stream
			 	try 
			 	{
					fstream.close();
				} 
			 	catch (IOException e)
				{
					e.printStackTrace();
				}
		 }

	}
}
